// pages/myactivities/myactivities.js
//获取应用实例
const app = getApp()
const util = require('../../utils/timeTool.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    delindex:null,
    choice:'',
    screen:'筛选',
    isScreen:false,
    screenList:['全部','已拒绝','审核中','报名中','报名已截止','已开始','已结束'],
    type: 0,
    type1: 0,
    pageNumber: 1,
    pageSize: 10,
    listtotalPages: '',
    deletetotalPages: '',
    pageNumber1: 1,
    pageSize1: 10,
    pageNumber2: 1,
    pageSize2: 10,
    drafttotalPages: '',
    delstatus: null,
    statusList: {
      '0': '未开始',
      '1': '进行中',
      '2': '已结束',
      '3': '已取消',
      '4': '未开始'
    },
    list: [],
    deleteList: [],
    draft: [],
    page: {
      type_0: {
        ifEmpty: false,
        "pageNumber": 1,
        "pageSize": 10,
        total: 0,
        totalPages: 0,
      },
      type_1: {
        ifEmpty: false,
        "pageNumber": 1,
        "pageSize": 10,
        total: 0,
        totalPages: 0,
      },
      type_2: {
        ifEmpty: false,
        "pageNumber": 1,
        "pageSize": 10,
        total: 0,
        totalPages: 0,
      },
      type_3: {
        ifEmpty: false,
        "pageNumber": 1,
        "pageSize": 10,
        total: 0,
        totalPages: 0,
      },
      // type_4: {
      //   ifEmpty: false,
      //   "pageNumber": 1,
      //   "pageSize": 10,
      //   total: 0,
      //   totalPages: 0,
      // },
    },
    userInfo: {},
    activesList_type_0: [],
    activesList_type_1: [],
    activesList_type_2: [],
    activesList_type_3: [],
    // activesList_type_4: [],
    ids: [],
  },
  isscreen(){
    this.setData({
      isScreen:true
    })
  },
  hidedel(){
    this.toggle("delindex", null);
  },
  showdel(e) {
    var index = e.currentTarget.dataset.index
    console.log('index', index)
    this.toggle("delindex", index);
  },
  orgChoice(e){
    var index = e.currentTarget.dataset.index
    if(index==0){
      index=''
    }else{
      index=index-1
    }
    console.log(index)
    this.setData({
      choice:''+index,
      screen: this.data.screenList[e.currentTarget.dataset.index],
      isScreen:false,
      pageNumber: 1,
      pageSize: 10,
      list: [],
    })
    this.getlist()
  },
  jumpToPage: function(e) {
    var acttype = e.currentTarget.dataset.acttype;
    var id = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type
    console.log(e)
    console.log("点击", id, this.data.type)
    if (id) {
      if (acttype == 12) {
        wx.navigateTo({
          url: '/pages/partySettingMemo/partySettingMemo?id=' + id
        })
      } else if (this.data.type == 2) {
        wx.navigateTo({
          url: '/pages/party/party?id=' + id
        })
      } else if ((type == 2 || type==3)&& acttype != 1) {
        wx.navigateToMiniProgram({
          appId: app.globalData.blessingAppId,
          envVersion: app.globalData.activityMiniEnvVersion,
          path: 'pages/hundredDetailed/hundredDetailed?id=' + id + '&type=' + acttype,
          success(res) {
            console.log(res);
          }
        })
      } else if (type == 2 && acttype == 1) {
        wx.navigateToMiniProgram({
          appId: app.globalData.blessingAppId,
          envVersion: app.globalData.activityMiniEnvVersion,
          path: 'pages/ordinaryDetails/ordinaryDetails?id=' + id,
          success(res) {
            console.log(res);
          }
        })
      } else {
        wx.navigateTo({
          url: '/pages/register/register?id=' + id
        })
      }

    }
  },
  preventTouchMove(){},
  formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  },
  formatTime(dateTime) {
    var date = new Date((dateTime.replace(/\-/g, "/")));
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    var hour = date.getHours();
    var minute = date.getMinutes();
    hour = (hour < 10 ? "0" + hour : hour)
    minute = (minute < 10 ? "0" + minute : minute)
    return year + '-' + month + '-' + day + ' ' + hour + ': ' + minute;
  },
  onChange(e) {
    this.setData({
      type: e.detail.index
    })
    this.setData({
      type1: e.detail.index
    })
    if (e.detail.index == 0) {
      this.setData({
        pageNumber: 1,
        pageSize: 10,
        list: [],
      })
      this.getlist()
    } else if (e.detail.index == 2) {
      this.setData({
        pageNumber1: 1,
        pageSize1: 10,
        draft: [],
      })
      this.getdraft()
    } else if (e.detail.index == 1) {
      this.setData({
        pageNumber2: 1,
        pageSize2: 10,
        deleteList: [],
      })
      this.getdelete()
    }
    this.toggle('page.type_' + this.data.type, {
      "pageNumber": 1,
      "pageSize": 10,
      total: 0,
      totalPages: 0,
    });
    this.toggle('activesList_type_' + this.data.type, []);
    // 埋点
    if (e.detail.index == 0) {
      app.TestBuriedPoint('yqlw_list_conduct', 'yqlw_list_pv')
      // app.TestBuriedPoint('yqlw_list_pv')
    } else if (e.detail.index == 1) {
      app.TestBuriedPoint('yqlw_list_notyetbegun', 'yqlw_list_pv')
      // app.TestBuriedPoint('yqlw_list_pv')
    } else if (e.detail.index == 2) {
      app.TestBuriedPoint('yqlw_list_draft', 'yqlw_list_pv')
      // app.TestBuriedPoint('yqlw_list_draft')
    } else if (e.detail.index == 3) {
      app.TestBuriedPoint('yqlw_list_history', 'yqlw_list_pv')
      // app.TestBuriedPoint('yqlw_list_pv')
    }
    // this.loadListData();
  },


  birthdayevent: function(option) {
    var activityId = option.currentTarget.dataset.title
    wx.navigateTo({
      url: '/pages/partySettingMemo/partySettingMemo?id=' + activityId
    });
  },
  //活动列表
  getlist() {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    app.req({
      url: 'wechat/activity/allActivity',
      data: {
        pageNumber: that.data.pageNumber,
        pageSize: that.data.pageSize,
        choice: that.data.choice
      },
      method: 'POST',
      success: function(res) {
        console.log('返回列表', res.data);
        if (res.code == 200) {
          for (let i = 0; i < res.data.rows.length; i++) {
            res.data.rows[i].actStartTime = util.formatTime4(res.data.rows[i].actStartTime);
          }
          that.setData({
            list: that.data.list.concat(res.data.rows),
            listtotalPages: res.data.totalPages
          })
          wx.hideLoading();
        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 已删除列表
  getdelete() {
    var that = this
    app.req({
      url: 'wechat/activity/userDeleteList',
      data: {
        pageNumber: that.data.pageNumber2,
        pageSize: that.data.pageSize2
      },
      method: 'POST',
      success: function(res) {
        if (res.code == 200) {
          for (let i = 0; i < res.data.rows.length; i++) {
            res.data.rows[i].actStartTime = util.formatTime4(res.data.rows[i].actStartTime);
          }
          that.setData({
            deleteList: that.data.deleteList.concat(res.data.rows),
            deletetotalPages: res.data.totalPages
          })
          console.log('已删除列表', that.data.deleteList);
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  touchS(e) {
    // 获得起始坐标
    this.startX = e.touches[0].clientX;
    this.startY = e.touches[0].clientY;
  },
  touchM(e) {
    var index = e.currentTarget.dataset.index
    // 获得当前坐标
    this.currentX = e.touches[0].clientX;
    this.currentY = e.touches[0].clientY;
    const x = this.startX - this.currentX; //横向移动距离
    const y = Math.abs(this.startY - this.currentY); //纵向移动距离，若向左移动有点倾斜也可以接受
    if (x > 35 && y < 110) {
      //向左滑是显示删除
      this.setData({
        delstatus: index
      })
    } else if (x < -35 && y < 110) {
      //向右滑
      this.setData({
        delstatus: null
      })
    }
  },

  loadListData: function() {
    var that = this;
    this.setData({
      'page.type_1.ifEmpty': false,
      'page.type_2.ifEmpty': false,
      'page.type_3.ifEmpty': false,
      // 'page.type_4.ifEmpty': false,
    })
    var url = '';
    if (that.data.type == 0) {
      url = 'wechat/activity/listActByStatus'
    } else {
      url = "wechat/activity/listActByStatus";
    }
    app.req({
      url: url,
      method: "post",
      data: {
        "status": that.data.type,
        "pageNumber": that.data.page['type_' + that.data.type].pageNumber,
        "pageSize": that.data.page['type_' + that.data.type].pageSize,
      },
      success: function(res) {
        if (res && res.code == 200) {
          console.log("this.data.type", that.data.type)
          if (url == "wechat/activity/selectActivity") {
            var arr = res.data.page.rows;
            for (let i = 0; i < arr.length; i++) {
              res.data.page.rows[i].statusName = that.data.statusList[arr[i].status];
              res.data.page.rows[i].actStartTimeDate = that.formatTime(arr[i].actStartTime);
            }

            if (that.data.type == 0) {
              that.setData({
                'activesList_type_0': that.data.activesList_type_0.concat(res.data.page.rows),
                'page.type_0.totalPages': res.data.page.totalPages,
                'page.type_0.total': res.data.page.total,
              })
              if (that.data.activesList_type_0.length) {
                that.setData({
                  'page.type_0.ifEmpty': false,
                })
              } else {
                that.setData({
                  'page.type_0.ifEmpty': true,
                })
              }
            } else if (that.data.type == 1) {
              that.setData({
                'activesList_type_1': that.data.activesList_type_1.concat(res.data.page.rows),
                'page.type_1.totalPages': res.data.page.totalPages,
                'page.type_1.total': res.data.page.total,
              })
              if (that.data.activesList_type_1.length) {
                that.setData({
                  'page.type_1.ifEmpty': false,
                })
              } else {
                that.setData({
                  'page.type_1.ifEmpty': true,
                })
              }
            } else if (that.data.type == 2) {
              that.setData({
                'activesList_type_2': that.data.activesList_type_2.concat(res.data.page.rows),
                'page.type_2.totalPages': res.data.page.totalPages,
                'page.type_2.total': res.data.page.total,
              })
              if (that.data.activesList_type_2.length) {
                that.setData({
                  'page.type_2.ifEmpty': false,
                })
              } else {
                that.setData({
                  'page.type_2.ifEmpty': true,
                })
              }
            } else if (that.data.type == 3) {
              that.setData({
                'activesList_type_3': that.data.activesList_type_3.concat(res.data.page.rows),
                'page.type_3.totalPages': res.data.page.totalPages,
                'page.type_3.total': res.data.page.total,
              })
              if (that.data.activesList_type_3.length) {
                that.setData({
                  'page.type_3.ifEmpty': false,
                })
              } else {
                that.setData({
                  'page.type_3.ifEmpty': true,
                })
              }
            }
          } else {
            console.log(res)
            console.log("--来了laodia ", that.data.type)
            var arr = res.data.rows;
            for (let i = 0; i < arr.length; i++) {
              res.data.rows[i].statusName = that.data.statusList[arr[i].status];
              res.data.rows[i].actStartTimeDate = that.formatTime(arr[i].actStartTime);
            }
            if (that.data.type == 0) {
              that.setData({
                'activesList_type_0': that.data.activesList_type_0.concat(res.data.rows),
                'page.type_0.totalPages': res.data.totalPages,
                'page.type_0.total': res.data.total,
              })
              if (that.data.activesList_type_0.length) {
                that.setData({
                  'page.type_0.ifEmpty': false,
                })
              } else {
                that.setData({
                  'page.type_0.ifEmpty': true,
                })
              }
            } else if (that.data.type == 1) {
              that.setData({
                'activesList_type_1': that.data.activesList_type_1.concat(res.data.rows),
                'page.type_1.totalPages': res.data.totalPages,
                'page.type_1.total': res.data.total,
              })
              if (that.data.activesList_type_1.length) {
                that.setData({
                  'page.type_1.ifEmpty': false,
                })
              } else {
                that.setData({
                  'page.type_1.ifEmpty': true,
                })
              }
            } else if (that.data.type == 2) {
              that.setData({
                'activesList_type_2': that.data.activesList_type_2.concat(res.data.rows),
                'page.type_2.totalPages': res.data.totalPages,
                'page.type_2.total': res.data.total,
              })
              if (that.data.activesList_type_2.length) {
                that.setData({
                  'page.type_2.ifEmpty': false,
                })
              } else {
                that.setData({
                  'page.type_2.ifEmpty': true,
                })
              }
            } else if (that.data.type == 3) {
              that.setData({
                'activesList_type_3': that.data.activesList_type_3.concat(res.data.rows),
                'page.type_3.totalPages': res.data.totalPages,
                'page.type_3.total': res.data.total,
              })
              if (that.data.activesList_type_3.length) {
                that.setData({
                  'page.type_3.ifEmpty': false,
                })
              } else {
                that.setData({
                  'page.type_3.ifEmpty': true,
                })
              }
            }
          }
        }
      }
    });
  },


  //去签到
  toSignIn: function(e) {


  },

  //草稿列表
  getdraft() {
    var that = this
    app.req({
      url: 'wechat/activity/listActByStatus',
      data: {
        status: 2,
        pageNumber: that.data.pageNumber1,
        pageSize: that.data.pageSize1,
      },
      method: 'POST',
      success: function(res) {
        console.log('返回草稿列表', res.data);
        if (res.code == 200) {
          for (let i = 0; i < res.data.rows.length; i++) {
            res.data.rows[i].actStartTime = util.formatTime4(res.data.rows[i].actStartTime);
          }
          that.setData({
            draft: that.data.draft.concat(res.data.rows),
            drafttotalPages: res.data.totalPages
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  //草稿删除
  deletebtn(e) {
    var that = this
    console.log(e)
    var id = e.currentTarget.dataset.id
    that.data.ids.push(id)
    that.setData({
      ids: that.data.ids
    })
    console.log('草稿删除', id, that.data.ids)
    app.req({
      url: 'wechat/activity/deleteStorage',
      data: {
        ids: that.data.ids
      },
      method: 'POST',
      success: function(res) {
        console.log('返回', res);
        if (res.code == 200) {
          that.setData({
            ids: []
          })
          that.onShow()
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 活动删除
  del(e) {
    var that = this
    var actid = e.currentTarget.dataset.id
    var det = e.currentTarget.dataset
    var type = ''
    console.log('删除', det)
    if (e.currentTarget.dataset.type == 1) {
      type = 0
    } else if (det.type == 2 && det.acttype == 2) {
      type = 2
    }
    if (det.type == 2 && det.acttype == 3) {
      type = 3
    }
    wx.showModal({
      content: '请确认是否删除',
      cancelText: "取消",
      confirmText: "确认",
      confirmColor: '#3680FE',
      cancelColor: '#333333',
      success(res) {
        if (res.confirm) {
          app.req({
            url: 'wechat/activity/userDeleteAct',
            data: {
              actId: actid,
              type: type
            },
            method: 'POST',
            success: function(res) {
              console.log('返回', res);
              if (res.code == 200) {
                wx.showToast({
                  title: '删除成功',
                  duration: 2000
                })
                that.setData({
                  delstatus: null
                })
                that.onShow()
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        }
      }
    })
  },
  // 活动恢复
  recovery(e) {
    var that = this
    var actid = e.currentTarget.dataset.id
    wx.showModal({
      content: '请确认是否恢复',
      cancelText: "取消",
      confirmText: "确认",
      confirmColor: '#3680FE',
      cancelColor: '#333333',
      success(res) {
        if (res.confirm) {
          app.req({
            url: 'wechat/activity/userRestoreAct',
            data: {
              id: actid
            },
            method: 'POST',
            success: function(res) {
              console.log('返回', res);
              if (res.code == 200) {
                wx.showToast({
                  title: '恢复成功',
                  duration: 2000
                })
                that.setData({
                  delindex: null
                })
                that.onShow()
              } else {
                that.setData({
                  delindex: null
                })
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        }
      }
    })
  },
  // 彻底删除
  thoroughdel(e){
    var that = this
    var actid = e.currentTarget.dataset.id
    wx.showModal({
      content: '彻底删除后，您将无法恢复此内容，请确认是否彻底删除',
      cancelText: "取消",
      confirmText: "确认",
      confirmColor: '#3680FE',
      cancelColor: '#333333',
      success(res) {
        if (res.confirm) {
          app.req({
            url: 'wechat/activity/userCompleteAct',
            data: {
              id: actid
            },
            method: 'POST',
            success: function (res) {
              console.log('返回', res);
              if (res.code == 200) {
                wx.showToast({
                  title: '彻底删除成功',
                  duration: 2000
                })
                that.setData({
                  delindex: null
                })
                that.onShow()
              } else {
                that.setData({
                  delindex: null
                })
                wx.showToast({
                  title: res.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        }
      }
    })
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log("options", options)
    var that = this;
    that.setData({
      type: 0,
    })
    console.log("onLoad", that.data.type)
    app.getUserInfo(function(userInfo) {
      that.toggle("userInfo", userInfo);
      // that.loadListData();

    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function(options) {
    //this.onPullDownRefresh()
    // var data = this.data;
    // console.log("onShow", data.type)
    // this.toggle('page.type_' + data.type, {
    //   "pageNumber": 1,
    //   "pageSize": 10,
    //   total: 0,
    //   totalPages: 0,
    // });
    // this.toggle('activesList_type_' + data.type, []);
    // this.loadListData();
    this.setData({
      list: [],
      draft: [],
      deleteList: [],
      pageNumber: 1,
      pageSize: 10,
      pageNumber1: 1,
      pageSize1: 10,
      pageNumber2: 1,
      pageSize2: 10,
    })
    if (this.data.type1 == 0) {
      this.getlist()
    } else if (this.data.type1 == 2) {
      this.getdraft()
    } else if (this.data.type1 == 1) {
      this.getdelete()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    // var data = this.data;
    // this.toggle('page.type_' + data.type, {
    //   "pageNumber": 1,
    //   "pageSize": 10,
    //   total: 0,
    //   totalPages: 0,
    // }, );
    // this.toggle('activesList_type_' + data.type, []);
    //   this.loadListData();
    if (this.data.type1 == 0) {
      this.setData({
        pageNumber:1,
        list: []
      })
      this.getlist()
    } else if (this.data.type1 == 2) {
      this.setData({
        pageNumber1:1,
        draft: []
      })
      this.getdraft()
    } else if (this.data.type1 == 1) {
      this.setData({
        pageNumber2:1,
        deleteList: []
      })
      this.getdelete()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    // var data = this.data;
    // if (data.page['type_' + data.type].totalPages > data.page['type_' + data.type].pageNumber) {
    //   this.toggle('page.type_' + data.type + '.pageNumber', data.page['type_' + data.type].pageNumber + 1);
    //     this.loadListData();
    // }
    if (this.data.type1 == 0 && this.data.listtotalPages >= this.data.pageNumber) {
      this.setData({
        pageNumber: this.data.pageNumber + 1,
      })
      this.getlist()
    } else if (this.data.type1 == 1 && this.data.deletetotalPages >= this.data.pageNumber2) {
      this.setData({
        pageNumber2: this.data.pageNumber2 + 1,
      })
      this.getdelete()
    } else if (this.data.type1 == 2 && this.data.drafttotalPages >= this.data.pageNumber1) {
      this.setData({
        pageNumber1: this.data.pageNumber1 + 1,
      })
      this.getdraft()
    }
  },
})